﻿using Quartz.Impl;
using Quartz;
using System.Collections.Specialized;
using WeskyQuantDemo.Models.Quartz;
using WeskyQuantDemo.Services.Jobs;

namespace WeskyQuantDemo.HostServices
{
    public class QuartzJobInitHostService : IHostedService, IDisposable
    {
        private readonly ISchedulerFactory _schedulerFactory;
        public QuartzJobInitHostService(ISchedulerFactory schedulerFactory)
        {
            _schedulerFactory = schedulerFactory;
        }
        public Task StartAsync(CancellationToken stoppingToken)
        {

            var properties = new NameValueCollection();
            properties["quartz.scheduler.instanceName"] = "定时任务监控";

            // 设置线程池
            properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
            properties["quartz.threadPool.threadCount"] = QuartzCongifurationInfo.ThreadPool;
            properties["quartz.threadPool.threadPriority"] = QuartzCongifurationInfo.Priority;

            // 远程输出配置
            properties["quartz.scheduler.exporter.type"] = "Quartz.Simpl.RemotingSchedulerExporter, Quartz";
            properties["quartz.scheduler.exporter.port"] = QuartzCongifurationInfo.Ip;
            properties["quartz.scheduler.exporter.bindName"] = QuartzCongifurationInfo.BindName;
            properties["quartz.scheduler.exporter.channelType"] = QuartzCongifurationInfo.ChannelType;

            var scheFactory = _schedulerFactory as StdSchedulerFactory;
            scheFactory.Initialize(properties);

            JobSchedulerInfo.JobsScheduler = scheFactory.GetScheduler().Result;
            int count = 2; // 任务数量

            IJobDetail[] jobs = new IJobDetail[count];
            ITrigger[] triggers = new ITrigger[count];
            for(int i = 0; i < count ; i++)
            {
                if (i == 0)
                {
                    // 定时任务1
                    jobs[i] = JobBuilder.Create<GetCurrentPriceJob>()
                     .WithIdentity($"Job_GetCurrentPrice", $"获取实时股票价格")
                     .Build();
                    triggers[i] = TriggerBuilder.Create()
                    .WithIdentity($"Job_GetCurrentPrice", $"获取实时股票价格~")
                    .StartNow()
                    .WithCronSchedule("30 29,59 9,10,11,13,14,15 ? * * ")
                    .Build();

                }
                else if (i == 1)
                {
                    // 定时任务2
                    jobs[i] = JobBuilder.Create<GetHistoryPriceJob>()
                     .WithIdentity($"Job_GetHistoryPriceJob", $"获取历史股票数据")
                     .Build();
                    triggers[i] = TriggerBuilder.Create()
                    .WithIdentity($"Job_GetHistoryPriceJob", $"获取历史股票数据~")
                    .StartNow()
                    .WithCronSchedule("30 1 9 ? * * ")
                    .Build();

                }

                JobSchedulerInfo.JobsScheduler.ScheduleJob(jobs[i], triggers[i]);

            }

            JobSchedulerInfo.JobsScheduler.Start();

            return Task.CompletedTask;
        }


        public Task StopAsync(CancellationToken stoppingToken)
        {

            return Task.CompletedTask;
        }

        public void Dispose()
        {

        }
    }
}
